package yingyong;

import gongju.DBUtil;
import xuesheng.Student;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet(name = "update", value = "/update")
public class update extends HttpServlet {
    //讲要修改的数据从数据库里面调取出来
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String id = request.getParameter("id");
        String sql = "select * from student where id = ?";//执行的语句
        try {
            ResultSet re = DBUtil.query(sql, id);//结果集
            re.next();//如果没写会显示sql. sqlexception:在结果集开始之前
            int id1 = re.getInt("id");
            String name = re.getString("name");
            String sex = re.getString("sex");
            Student st = new Student(id1, name, sex);
            request.setAttribute("student",st);//封装好的学生类
            request.getRequestDispatcher("/edit.jsp").forward(request,response);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
